package io.gitee.jinceon;

import io.gitee.jinceon.parser.SimpleTreeBaseVisitor;
import io.gitee.jinceon.parser.SimpleTreeParser;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.List;

public class SimpleTreeFlattenVisitor extends SimpleTreeBaseVisitor<Void> {

    private final Deque<String> stack = new ArrayDeque<>(List.of(""));
    private final List<Node> result;

    public SimpleTreeFlattenVisitor(List<Node> result) {
        this.result = result;
    }

    @Override
    public Void visitObject(SimpleTreeParser.ObjectContext ctx) {
        String text = ctx.label().getText();
        System.out.println("\t".repeat(stack.size()) +text);
        String parent = stack.peekLast();
        stack.addLast(text);
        result.add(new Node(text, parent));
        visitChildren(ctx.collection());
        stack.removeLast();
        return null;
    }

    @Override
    public Void visitStringLiteral(SimpleTreeParser.StringLiteralContext ctx) {
        String text = ctx.getText();
        System.out.println("\t".repeat(stack.size()) + text);
        String parent = stack.peekLast();
        result.add(new Node(text, parent));
        return null;
    }
}

